<?php 
namespace Home\Controller;

use Think\Controller;
class UserController extends Controller {
	static private $user;

	//构造函数
	public function __construct(){
		//继承父类构造函数,防止修改父类构造方法
		parent::__construct();
		//创建连接数据库对象
		self::$user =  D('Users');
	}

	//用户注册页面
	public function register(){
		//如果检测到登陆则跳转到首页
		if(cookie('Qkey')){
			$this->redirect("/");
			exit;
		}
		if(IS_POST){
			//创建UsersModel的实例
			$user = self::$user;
			if(!$user->create()){//自动验证
				$return = ['success'=>false,'msg'=>$user->getError()];//自动验证不通过返回结果
			}else{
				//密码加盐+盐保存
				if($_POST['type'] == 1){
					//如果为企业用户则把账号状态置为待审核
					$user->accstatus = '0';
				}
				$salt = $this->creatSalt();//生成随机盐
				$user->password = md5($user->password.$salt);//密码加盐更安全
				$user->salt = $salt; //保存随机盐
				$user->acctype = $_POST['type'];//保存注册者的身份
				$res = $user->add();	
				$return = ['success'=>true,'msg'=>$salt]; //注册成功返回结果给前台
			}   

			echo json_encode($return);//给前台返回json格式的结果
			exit;
		}else{
			$this->display();//没POST就显示模板
		}
	}

	//用户登陆页面
	public function login(){
		//如果检测到登陆并检测COOKIE正确则跳转到首页
		if(cookie('Qkey')){
			$this->redirect("/");
			exit;
		}
		if(IS_POST){
			$user = self::$user;
			$email = I('post.email');
			$users = $user->where(['email'=>$email])->find();
			if(!$users){
				$return = ['success'=>false,'msg'=>'邮箱没有找到,请确定是否注册!'];
			}else{
				$password = md5(I('post.password').$users['salt']);
				if($users['password'] == $password){
					if($users['nick'] == ''){
						$username = substr($email,0,strpos($email,'@'));
					}else{
						$username = $users['nick'];
					}
					//cookie加盐确保安全还有安全KEY加密顺序
					$key = md5($users['password'].$users['uid'].$users['acctype'].$users['accstatus'].C('C_SALT').$username);
					cookie('uid',$users['password']);
					cookie('sn',$users['uid']);
					cookie('Utype',$users['acctype']);
					cookie('Usta',$users['accstatus']);
					cookie('Uname',$username);
					cookie('Qkey',$key);
					$logintime = ['logintime'=>time()];
					$user->where(['email'=>$email])->save($logintime);
					$return = ['success'=>true];
				}else{
					$return = ['success'=>false,'msg'=>'密码错误!'];
				}
			}
			echo json_encode($return);
			exit;
		}else{	
			$this->display('login');
		}
	}

	//退出登陆
	public function logout(){
		cookie('uid',null);
		cookie('sn',null);
		cookie('Utype',null);
		cookie('Usta',null);
		cookie('Qkey',null);
		cookie('Uname',null);
		$this->success('退出成功!','/');
	}

	/**
	* 生成随机密码盐
	*/
	public function creatSalt(){
		$str = substr(str_shuffle('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'),0,8);
		return $str;
	}

	//用户中心
	public function account(){
		//权限检测:检测登陆并检测COOKIE是否正确
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		$user = self::$user;
		if(IS_POST){
			$user->nick = $_POST['nick'];
			$user->mobile = $_POST['mobile'];
			$res = $user->where(['uid'=>cookie('sn')])->save();
			if($res){
				$return = ['success'=>true];
			}else{	
				$return = ['success'=>false];
			}
			exit(json_encode($return) );
		}else{
			$userinfo = $user->where(['uid'=>cookie('sn')])->find();
			$this->assign('userinfo',$userinfo);
			$this->display();
		}
	}


	//修改密码
	public function updatePwd(){
		//权限检测:检测登陆并检测COOKIE是否正确
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		$user = self::$user;
		$userinfo = $user->where('uid='.cookie('sn'))->find();
		if(IS_POST){
			$password = md5($_POST['oldPassword'].$userinfo['salt']);
			if($password == $userinfo['password']){
				if($_POST['newPassword'] == $_POST['newPassword2']){
					$user->password = md5($_POST['newPassword'].$userinfo['salt']);
					$res = $user->where('uid='.cookie('sn'))->save();
					if($res){
						$return = ['success'=>true,'msg'=>'密码修改成功!'];
					}else{
						$return = ['success'=>false,'msg'=>'内部错误,密码修改失败!'];
					}
				}else{
					$return = ['success'=>false,'msg'=>'两次密码输入不一致!'];
				}
			}else{
				$return = ['success'=>false,'msg'=>'原密码错误!'];
			}
			cookie('Uname',null);	
			exit(json_encode($return));
		}else{
			$this->assign('userinfo',$userinfo);
			$this->display();
		}
	}

	//已投递简历
	public function resumes_post(){
		//权限检测:检测登陆并检测COOKIE是否正确
		if(cookie('Qkey')){
			if(!homechcekck()){
				exit($this->error('信息不正确,请重新登陆!','/',1) );
			}
		}else{
			exit($this->error('请登陆!','/Home/User/login',1));
		}
		//检测账号类型
		if(cookie('Utype') != 0){
			exit($this->error('账号类型错误!','javascript:history.back(-1);',1));
		}
		if(isset($_GET['id'])){
			$id = I('id');
			if($id==1){
				$uid = cookie('sn');
				$count = D('resumes_post as pres')->where(['pres.uid'=>$uid,'pres.status'=>$id])->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->count();
				$Page = new \Think\Page($count,2);
				$show = $Page->show();
				$postres = D('resumes_post as pres')->where(['pres.uid'=>$uid,'pres.status'=>$id])->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->field('com.com_id,com.comname,com.com_city,pres.rid,pres.uid,pres.pid,pres.com_id,pres.job_id,pres.positionName,pres.status,pres.posttime,pres.updatetime,job.salaryMin,job.salaryMax')->limit($Page->firstRow.','.$Page->listRows)->select();
				//var_dump($postres);exit;
				$this->assign('count',$count);
				$this->assign('page',$show);
				$this->assign('pres',$postres);
				$this->display('delivery');
				exit;
			}elseif($id==2){
				$uid = cookie('sn');
				$count = D('resumes_post as pres')->where(['pres.uid'=>$uid,'pres.status'=>$id])->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->count();
				$Page = new \Think\Page($count,2);
				$show = $Page->show();
				$postres = D('resumes_post as pres')->where(['pres.uid'=>$uid,'pres.status'=>$id])->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->field('com.com_id,com.comname,com.com_city,pres.rid,pres.uid,pres.pid,pres.com_id,pres.job_id,pres.positionName,pres.status,pres.posttime,pres.updatetime,job.salaryMin,job.salaryMax')->limit($Page->firstRow.','.$Page->listRows)->select();
				//var_dump($postres);exit;
				$this->assign('count',$count);
				$this->assign('page',$show);
				$this->assign('pres',$postres);
				$this->display('delivery');
				exit;
			}else{
				exit($this->error('参数非法,您是从哪来的?!','javascript:history.back(-1);',1));
			}
		}
		//没传参的情况下
		//实例化对象
		$uid = cookie('sn');
		$count = D('resumes_post as pres')->where('pres.uid='.$uid)->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->count();
		$Page = new \Think\Page($count,2);
		$show = $Page->show();
		$postres = D('resumes_post as pres')->where('pres.uid='.$uid)->join('v5_companys as com ON pres.com_id = com.com_id')->join('v5_jobs as job ON pres.job_id = job.job_id')->field('com.com_id,com.comname,com.com_city,pres.rid,pres.uid,pres.pid,pres.com_id,pres.job_id,pres.positionName,pres.status,pres.posttime,pres.updatetime,job.salaryMin,job.salaryMax')->limit($Page->firstRow.','.$Page->listRows)->select();
		//var_dump($postres);exit;
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->assign('pres',$postres);
		$this->display('delivery');
	}

}
